import { Constants } from '../common/Constants';
import DatabaseHelper from './DatabaseHelper';
import { chaplog } from '../models/chaplog';

export class QATopModel {
  headImg: string; // 头像url
  accoutName: string; // 账号名称
  score: number; // 得分

  constructor(chaplog: chaplog, imgurl: string) {
    this.headImg = imgurl;
    this.accoutName = chaplog.remark;
    this.score = chaplog.score;
  }
}

export class RankingServices {
  // 获取首页上的排行榜
  static async Get_Ranking_Home(pageIndex: number,pageSize:number):Promise<QATopModel[]> {
    const list = await DatabaseHelper.Home_Query_By_Page(pageIndex,pageSize);
    const uids = list.map(item => item.uid);
    const headImgs = await DatabaseHelper.userHeadImg_Query_By_uids(uids);
    let qlist = [];
    list.forEach((chaplog, index: number) => {
      let userHead = headImgs.find(u => u.uid === chaplog.uid);
      let randnum = Math.floor(Math.random() * Constants.USER_HEAD_IMGURLS.length);
      let imgurl = userHead ? userHead.imgurl : Constants.USER_HEAD_IMGURLS[randnum];
      let eindex = chaplog.remark.indexOf('@');
      let accoutName = chaplog.remark;
      if (eindex > -1) {
        // 表示当前账号是邮箱
        // chaplog.remark = chaplog.remark.slice(0,eindex)
        let preChars = eindex > 3 ? chaplog.remark.slice(0, 2) : chaplog.remark.slice(0, 1);
        let nextChars = chaplog.remark.slice(eindex, chaplog.remark.length);
        accoutName = preChars +"****"+nextChars;
      }else{
        let preChars = chaplog.remark.slice(0, 3);
        let nextChars = chaplog.remark.slice(7, 11);
        accoutName = preChars +"****"+nextChars;
      }
      chaplog.remark = accoutName;
      qlist.push(new QATopModel(chaplog, imgurl));
    });

    return qlist;
  }

}
